package com.admin.kotlintest.utils

import rx.functions.Action1
import com.admin.kotlintest.bean.Installation
import cn.bmob.v3.BmobInstallationManager
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.BmobUser
import com.admin.kotlintest.bean.MyUser
import com.socks.library.KLog


/**
 * Created by 吴昶 on 2018/4/20.
 */
class SystemUtils{

    companion object {

        fun initPush(){
            var user=BmobUser.getCurrentUser(MyUser::class.java)
            val bmobQuery = BmobQuery<Installation>()
            val id = BmobInstallationManager.getInstallationId()
            bmobQuery.addWhereEqualTo("installationId", id)
            bmobQuery.findObjectsObservable(Installation::class.java)
                    .subscribe({ installations ->
                        if (installations.size > 0) {
                            val installation = installations[0]
                            installation.myUser=user
                            installation.updateObservable()
                                    .subscribe({ KLog.d("更新设备用户信息成功！") }) { throwable -> KLog.d("更新设备用户信息失败：" + throwable.message) }

                        } else {
                            KLog.d("后台不存在此设备Id的数据，请确认此设备Id是否正确！\n$id")
                        }
                    }) { throwable -> KLog.d("查询设备数据失败：" + throwable.message) }
        }

        fun installationMsgFormat(title:String,msg:String) :String {
            var msg="{\"title\":\"$title\",\"msg\":\"$msg\",\"action\":\"com.kotlin.APPIONTMENTRECORD\"}"
            return msg
        }
    }
}